<?php
class Content_Model extends CI_Model {

	var $id;
	var $gmt_create;
	var $creator;
	var $gmt_modified;
	var $modifier;
	var $category_id;
	var $content;
	var $order_no = 0;
	var $recommend = 0;
	var $title;
	var $picture_url;

	public function __construct() {
		$this->load->database();
	}

	/**
	 * 按栏目id查询文章
	 */
	public function getByCategoryId($categoryId) {
		$sql = " SELECT content.* , admin.user_name FROM content ";
		$sql.= " JOIN admin ON content.creator = admin.id ";
		$sql.= " WHERE content.category_id = " . $categoryId . " ORDER BY content.order_no";

		$query = $this->db->query($sql);
		return $query->result_array();
	}

	public function createContent() {

		$this->db->insert('content', $this);

	}

	public function updateContent($content) {

		$this->db->where('id', $content->id)->update('content', $content);
	}

	public function get($content_id){
		$query = $this->db->get_where('content', array('id'=>$content_id));
		return $query->row();
	}

	public function delete($content_id) {

		/*查看栏目下是否有文章，有文章则不让删除
		$content_num = $this->db->get_where('content', array('category_id' => $id));

		if($content_num > 0){
			return false ;
		}else{

			return true ;
		}
		*/

		$this->db->delete('content', array('id' => $content_id));

	}

	/**
	 * 在添加文章时获取最大的排序号并加1
	 */
	public function getMaxOrderNo($categoryId) {

		$sql = " SELECT MAX(order_no) AS num FROM content ";
		$sql.= " WHERE category_id = " . $categoryId ;

		$query = $this->db->query($sql);
		if ($query->num_rows() > 0){
		   $row = $query->row();
		   return $row->num + 1 ;
		}else{
			return 1 ;
		}
	}
}